<template>
  <!--答题结果-->
  <div>
    <headers></headers>
    <div class="padding88">
      <div class="question">
        <div class=" auto">
          <div class="header">
            服务能力<span>{{duan}}</span>
          </div>
          <div class="content">
            <p style="font-size: large;margin-bottom: 20px">本次答题：<span style="color: red">错误&nbsp;{{errorNum}}&nbsp;道</span>，<span style="color: green">正确&nbsp;{{rightNum}}&nbsp;道</span></p>
            <!--<div class="tab">
              <Button  size="large" :class="{active:iscur===0}" @click="iscur=0,tabChange(1)">正确题目 {{rightNum}}道</Button>
              <Button  size="large" :class="{active:iscur===1}" @click="iscur=1,tabChange(0)">错误题目 {{errorNum}}道</Button>
            </div>
            <ul class="list" v-if="tabView === 0">
              &lt;!&ndash;<div v-if="errors.length > 0">&ndash;&gt;
               <li v-for="an in errors" >
                 {{an.num}}、{{an.title}}
                 <p>正确答案：{{an.ropt}}</p>
                 <p class="error">错误答案：{{an.eopt}}</p>
               </li>
            </ul>
            <ul class="list" v-if="tabView === 1">
                <li v-for="an in rights">
                  {{an.num}}、{{an.title}}
                  <p>正确答案：{{an.opt}}</p>
                </li>
            </ul>-->
            <div class="caozuo">
              <Button  size="large" class="" @click="jiXu">继续答题</Button>
              <Button  size="large" class="" @click="fanHui">返回能力测评</Button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <Spin fix v-show="isSpinShow" >
      <div class="loader">
        <Icon type="ios-loading" size=26 class="demo-spin-icon-load"></Icon>
        <div class="font14">加载中...</div>
      </div>
    </Spin>
    <footers></footers>
  </div>
</template>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  /* 旋转效果 */
  .demo-spin-icon-load{
    animation: ani-demo-spin 1s linear infinite;
  }
  @keyframes ani-demo-spin {
    from { transform: rotate(0deg);}
    50% { transform: rotate(180deg);}
    to { transform: rotate(360deg);}
  }
  /* 假如内容过长，一屏放不下，滚动条下拉覆盖不全 */
  .ivu-spin-fix {
    position: fixed;
  }

  .list{
    li{
      font-size: 14px;
      color: #000;
      margin-bottom: 30px;
      p{
        color: #009900;
        margin-top: 10px;
      }
      .error{
        color: #ff3300;
      }
    }
  }

  .question{
    background-color: #f5f5f5;
    margin-bottom: -50px;
    padding:40px 0 250px;
    .auto{
      background-color: #fff;
      border-radius: 3px;
      padding: 30px;
      .header{
        border-bottom: 1px solid #dbdbdb;
        padding-bottom: 20px;
        font-size: 20px;
        color: #333;
        text-align: left;
        span{
          margin-left: 30px;
        }
      }
      .content{
        padding: 40px 0 30px;
        text-align: left;
        .tab{
          button{
            height: 60px;
            font-size: 16px;
            color: #000;
            width: 200px;
            margin-bottom: 40px;
          }
          button:first-child{
            margin-right: 30px;
          }
          .ivu-btn:active, .ivu-btn.active{
            background-color: #957960;
            color: #fff;
            border-color: #957960;
          }
          .ivu-btn:focus{
            box-shadow: none;
          }
        }
        .caozuo{
          button{
            height: 50px;
            width: 282px;
            color: #333;
            font-size: 16px;
          }
          button:first-child{
            background-color: #957960;
            color: #fff;
            margin-right: 20px;
          }
        }
      }

    }
  }
</style>
<script type="text/ecmascript-6">
  import headers from '../template/header'
  import footers from '../template/footer'
  import nodata from '../template/null'
  import {centreInfoWeb,abilityPage} from '../../http'
  export default {
    name: '',
    props: {},
    data() {
      return {
        isSpinShow:false,
        tabView: 1,         //结果展示
        //答题结果
        iscur: 0,
        questions:'',       //题目（created中接收）
        answers:'',         //答案（created中接收）
        duan:'',            //段位（created中接收）
        //答题结果分析
        rights:[],
        rightNum:0,
        errors:[],
        errorNum:0,
        right:{
          title:'',
          opt:'',
          num:0,
        },
        error:{
          title:'',
          ropt:'',
          eopt:'',
          num:0,
        }
      };
    },
    methods: {
      //用户继续答题
      jiXu(){
        this.$router.push({
          name:'ceping4'
        })
      },
      //用户返回测评页
      fanHui(){
        this.$router.push({
          name:'ceping'
        })
      },
      //结果查看
      tabChange:function(tab){
        this.tabView = tab;
      },
      //答题结果统计分析
      total(){
        for(let i = 0;i < this.answers.length;i++){
          if(this.answers[i].isTrue === 1){
            let right = {
              title: this.questions[i].examTitle,
              opt:this.questions[i].opts[this.answers[i].index].examOptionName ,
              num: this.questions[i].num,
            }
            this.rights.push(right);
            this.rightNum++;
          }else{
            let ropt ;
            let qopts = this.questions[i].opts;
            for(let j = 0;j<qopts.length;j++){
              if(qopts[j].correct === 1){
                ropt = qopts[j].examOptionName;
              }
            }
            let error = {
              title: this.questions[i].examTitle,
              ropt: ropt,
              eopt: this.questions[i].opts[this.answers[i].index].examOptionName,
              num: this.questions[i].num,
            }
            this.errors.push(error);
            this.errorNum++;
          }
        }
      },
      getJieguo(){
        //答题结果并非后台返回统计，前端做统计后直接展示，统计结果不做存储
        if(this.isSpinShow === false) {
          this.isSpinShow = true;
          //用户是否登录
          centreInfoWeb().then(res => {
            if (res.code === 200) {
              //用户是否购买人才能力测评
              abilityPage().then(res => {
                if (res.code === 200) {
                  if (res.data.isBuy === 1) {
                    this.isSpinShow = false;
                    this.questions = JSON.parse(this.$route.query.questions);       //题目接收并转换
                    this.answers = JSON.parse(this.$route.query.answers);           //答案接收并转换
                    this.duan = this.$route.query.duan;
                    if (this.answers.length > 0) {
                      this.total();
                    }
                  } else {
                    this.isSpinShow = false;
                    this.$Notice.warning({
                      title: "人才测评提醒",
                      desc: "请先购买人才能力测评",
                      duration: 3,
                    });
                    this.$router.push({
                      name: 'ceping'
                    })
                  }
                }else {
                  this.isSpinShow = false;
                  this.$Notice.warning({
                    title: "人才测评提醒",
                    desc: res.info,
                    duration: 3,
                  });
                }
              }).catch()
            } else {
              this.isSpinShow = false;
              this.$Notice.warning({
                title: '登录提醒 ',
                desc: '使用人才测评，需用户登录！ ',
                duration: 3,
              });
              this.$router.push({name: 'ceping'});
            }
          }).catch()
        }
      }
    },
    created() {
      this.getJieguo();
      /*this.$router.push({
        name:'ceping'
      })*/
    },
    components: {
      headers,
      footers,
      nodata
    }
  }
</script>
